package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.PrintTemplate;
import com.ruoyi.factorydata.service.IPrintTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 打印单模板Controller
 *
 * @author ruoyi
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/prinTemplate/template")
public class PrintTemplateController extends BaseController {
    @Autowired
    private IPrintTemplateService printTemplateService;

    /**
     * 查询打印单模板列表
     */
    @PreAuthorize("@ss.hasPermi('prinTemplate:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrintTemplate printTemplate) {
        startPage();
        List<PrintTemplate> list = printTemplateService.selectPrintTemplateList(printTemplate);
        return getDataTable(list);
    }

    /**
     * 导出打印单模板列表
     */
    @PreAuthorize("@ss.hasPermi('prinTemplate:template:export')")
    @Log(title = "打印单模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PrintTemplate printTemplate) {
        List<PrintTemplate> list = printTemplateService.selectPrintTemplateList(printTemplate);
        ExcelUtil<PrintTemplate> util = new ExcelUtil<PrintTemplate>(PrintTemplate.class);
        util.exportExcel(response, list, "打印单模板数据");
    }

    /**
     * 获取打印单模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('prinTemplate:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(printTemplateService.selectPrintTemplateById(id));
    }

    /**
     * 新增打印单模板
     */
    @PreAuthorize("@ss.hasPermi('prinTemplate:template:add')")
    @Log(title = "打印单模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrintTemplate printTemplate) {
        return toAjax(printTemplateService.insertPrintTemplate(printTemplate));
    }

    /**
     * 修改打印单模板
     */
    @PreAuthorize("@ss.hasPermi('prinTemplate:template:edit')")
    @Log(title = "打印单模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrintTemplate printTemplate) {
        return toAjax(printTemplateService.updatePrintTemplate(printTemplate));
    }

    /**
     * 删除打印单模板
     */
    @PreAuthorize("@ss.hasPermi('prinTemplate:template:remove')")
    @Log(title = "打印单模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(printTemplateService.deletePrintTemplateByIds(ids));
    }
}
